package it.avantsoft.ls.gl.message;

import java.io.DataOutputStream;
import java.io.IOException;

public class ConnectToGLMessage extends GroupLeaderMessage {
    private String childId;
    private String groupLeaderHost;
    private int groupLeaderPort;

    public ConnectToGLMessage(DataOutputStream out, String childId, String groupLeaderHost, int groupLeaderPort) {
        super(out);
        this.childId = childId;
        this.groupLeaderHost = groupLeaderHost;
        this.groupLeaderPort = groupLeaderPort;
    }

    public boolean send() {
        boolean sent = false;
        try {
            writeByte(CONNECT_TO);
            writeUTF(childId);
            writeUTF(groupLeaderHost);
            writeInt(groupLeaderPort);
            flush();
            sent = true;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return sent;
    }


    public String toString() {
        return "ConnectToGLMessage{" +
                "childId='" + childId + '\'' +
                ", groupLeaderHost='" + groupLeaderHost + '\'' +
                ", groupLeaderPort=" + groupLeaderPort +
                "}";
    }
}
